﻿using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using NetWork;
using ProtoBuf.Message;
namespace Room
{
    public class RoomNetTown : RoomNetBase
    {
        private RoomTown roomTown {
            get { return roomBase as RoomTown; }
        }


        public override void Tick()
        {
            base.Tick();
        }

        public override void Enter()
        {
            base.Enter();

        }

        public override void Leave()
        {
            base.Leave();
        }

        public override void RegistSocketHandler()
        {
            base.RegistSocketHandler();

            NetAPI.RegistSocketHandler(OpDefine.SCFieldPlayerState, SCFieldPlayerState);
            NetAPI.RegistSocketHandler(OpDefine.SCShowArround, SCShowArround);
        }

        public override void UnRegistSocketHandler()
        {
            base.UnRegistSocketHandler();
            NetAPI.UnregisterSocketHandler(OpDefine.SCFieldPlayerState, SCFieldPlayerState);
            NetAPI.UnregisterSocketHandler(OpDefine.SCShowArround, SCShowArround);
        }

        public override void Pause()
        {
            base.Pause();
        }
        
        #region Send

        public override void SendCSCurrentPosition(object sender)
        {
            CSCurrentPosition msg = new CSCurrentPosition();
            msg.showArround = StaticData.playerData.IsShowPlayer ? 1 : 0;
            NetAPI.SendSocket(OpDefine.CSCurrentPosition, msg);
        }
        

        #endregion Send

        #region Callback

        protected override void SCCurrentPosition(object obj)
        {
            base.SCCurrentPosition(obj);
            SCCurrentPosition msg = obj as SCCurrentPosition;
            var selfCharactor = roomTown.charactorManager.GetSelfCharactor();
            roomTown.smallMap.miniMapComponent.SetMajorCharactor(selfCharactor.transform);
            //selfCharactor.SetKillNum();
            selfCharactor.SetKillNum(msg.self.info.playerSide.kills);
            //selfCharactor.SetKillNum(30);
            //Other Players
            //foreach (MovePlayer player in msg.players)
            //{
            //    //QueueRoleAppear.Enqueue(player);
            //    if (player.info.playerId == playerid)
            //        continue;
            //    SolveRoleAppear(player);

            //    Debug.LogFormat("role appear playerid = {0}", player.info.playerId);
            //}
            roomTown.StartCoroutine(OnSelfInitComplete());

            roomTown.viewController.DetectView();
        }

        IEnumerator OnSelfInitComplete() {
            var selfCharactor = roomTown.charactorManager.GetSelfCharactor();
            while (selfCharactor.lod.IsLoading()) {
                yield return null;
            }
            roomTown.RoomLoadEnd();
        }

        public void SCFieldPlayerState(object obj)
        {
            SCFieldPlayerState msg = (SCFieldPlayerState)obj;
            foreach (var side in msg.player)
            {
                playerSideQueue.Enqueue(side);
            }
        }

        public void SCShowArround(object obj)
        {
            SCShowArround msg = obj as SCShowArround;
            Debug.Log("[SCShowArround] " + msg.code);
            StaticData.playerData.IsShowPlayer = msg.code == 1 ? true : false;
            if (!StaticData.playerData.IsShowPlayer)
            {
                roomBase.charactorManager.HideOtherPlayer();
            }
        }


        #endregion

        #region help


        #endregion

    }
}
